

/*
 * Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution. 
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met: 
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer. 
 *
 * - Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution. 
 *
 * - Neither the name of the <ORGANIZATION> nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software without 
 *   specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE. 
 *
 */

package cubrid.jdbc.driver;

import java.sql.*;
import java.io.*;

import cubrid.sql.CUBRIDOID;

public class CUBRIDBlob extends CUBRIDOID implements Blob
{
  /*
   * ======================================================================= |
   * CONSTANT VALUES
   * =======================================================================
   */

  /*
   * ======================================================================= |
   * CONSTRUCTOR
   * =======================================================================
   */

  public CUBRIDBlob (CUBRIDOID o)
  {
    super (o);
  }

  /*
   * ======================================================================= |
   * java.sql.Blob interface
   * =======================================================================
   */

  public long length () throws SQLException
  {
    return (gloSize ());
  }

  public byte[] getBytes (long pos, int length) throws SQLException
  {
    byte[] b = new byte[length];

    int read_len = gloRead (pos, length, b, 0);

    if (read_len < length)
      {
 byte[]cpbuf = new byte[read_len];
 System.arraycopy (b, 0, cpbuf, 0, read_len);
 b = cpbuf;
      }

    return b;
  }

  public InputStream getBinaryStream () throws SQLException
  {
    return (new CUBRIDGloInputStream (this));
  }

  public long position (byte[]pattern, long start) throws SQLException
  {
    return (gloBinarySearch (start, pattern, 0, pattern.length));
  }

  public long position (Blob pattern, long start) throws SQLException
  {
    return (position (pattern.getBytes (1, GLO_MAX_SEARCH_LEN), start));
  }

  public int setBytes (long pos, byte[]bytes) throws SQLException
  {
    return (setBytes (pos, bytes, 0, bytes.length));
  }

  public int setBytes (long pos, byte[]bytes, int offset, int len)
    throws SQLException
  {
    return (gloWrite (pos, bytes, offset, len));
  }

  public OutputStream setBinaryStream (long pos) throws SQLException
  {
    return (new CUBRIDGloOutputStream (this, (int) pos));
  }

  public void truncate (long len) throws SQLException
  {
    gloTruncate (len);
  }


/* JDK 1.6 */
  public void free () throws SQLException
  {
    throw new java.lang.UnsupportedOperationException ();

  }

/* JDK 1.6 */
  public InputStream getBinaryStream (long pos,
          long length) throws SQLException
  {
    throw new java.lang.UnsupportedOperationException ();
  }

}
